package uz.edo.app.client.core;

import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.extensions.ajax.markup.html.form.upload.UploadWebRequest;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebRequestCycleProcessor;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.protocol.http.request.CryptedUrlWebRequestCodingStrategy;
import org.apache.wicket.protocol.http.request.WebRequestCodingStrategy;
import org.apache.wicket.request.IRequestCodingStrategy;
import org.apache.wicket.request.IRequestCycleProcessor;
import org.apache.wicket.session.ISessionStore;
import org.apache.wicket.settings.IRequestCycleSettings;

import uz.edo.app.admin.core.AdminSession;
import uz.edo.app.client.ErrorPage;
import uz.edo.app.client.Home;
import uz.edo.app.client.Login;
import uz.edo.app.client.MyHttpSessionStore;
import uz.edo.app.client.SignOut;
import uz.edo.error.MyPageExpired;
import uz.edo.security.MyAuthenticatedPage;
import uz.edo.security.MyPackageResourceGuard;
import uz.edo.security.MyAuthorization;

public class MyApplication extends WebApplication{

    private WebSession session;
	private static Log _log = LogFactory.getLog(MyApplication.class);

    public MyApplication() {  }
    protected void init()
    {
        super.init();

        getResourceSettings().setThrowExceptionOnMissingResource(true);
        getRequestCycleSettings().setRenderStrategy(IRequestCycleSettings.REDIRECT_TO_RENDER);
        getMarkupSettings().setStripWicketTags(true);
        getMarkupSettings().setStripXmlDeclarationFromOutput(true);
        getMarkupSettings().setCompressWhitespace(true);
        getMarkupSettings().setDefaultAfterDisabledLink("");
        getMarkupSettings().setDefaultBeforeDisabledLink("");
        getPageSettings().setVersionPagesByDefault(true);
        getPageSettings().setAutomaticMultiWindowSupport(false);
        MyPackageResourceGuard resourceGuard = new MyPackageResourceGuard();
        getDebugSettings().setAjaxDebugModeEnabled(false);
        resourceGuard.addBlockedExtension("xml");
        getResourceSettings().setPackageResourceGuard(resourceGuard);
        getSecuritySettings().setAuthorizationStrategy(new IAuthorizationStrategy()
        {
            public boolean isActionAuthorized(Component component, Action action)
            {
                if (MyAuthorization.isActionAuthorized(component, action, ((MySession) Session.get()).getUser()))
//                if (((MySession) Session.get()).isLoged())
                    return true;
                else
                    return false;
            }
            @SuppressWarnings("unchecked")
			public boolean isInstantiationAuthorized(Class componentClass)
            {
                if (MyAuthenticatedPage.class.isAssignableFrom(componentClass))
                {
                    if (((MySession) Session.get()).isLoged())
                    {
                        return true;
                    }
                    throw new RestartResponseAtInterceptPageException(Login.class);
                }
                return true;
            }
        });
        mountBookmarkablePage("/signout", SignOut.class);
        mountBookmarkablePage("/login", Login.class);
        mountBookmarkablePage("/home", Home.class);
        getApplicationSettings().setPageExpiredErrorPage(MyPageExpired.class);
    }
    public Session newSession(Request request, Response response)
    {
        return new MySession(request);
    }
    @SuppressWarnings("unchecked")
	public Class getHomePage()
    {
        return Home.class;
    }
    protected WebRequest newWebRequest(HttpServletRequest servletRequest)
    {
        return new UploadWebRequest(servletRequest);
    }
    protected IRequestCycleProcessor newRequestCycleProcessor()
    {
        return new WebRequestCycleProcessor()
        {
            protected IRequestCodingStrategy newRequestCodingStrategy()
            {
                return new CryptedUrlWebRequestCodingStrategy(new WebRequestCodingStrategy());
            }
        };
    }
    protected Page onRuntimeException(Page page, RuntimeException e)
    {
        return new ErrorPage(page, e);
    }
    protected ISessionStore newSessionStore()
    {
        return new MyHttpSessionStore(this);
    }


}
